//
//  HGGunCollectionViewCell.m
//  悦享
//
//  Created by dllo on 16/3/18.
//  Copyright © 2016年 张洪国. All rights reserved.
//

#import "HGGunCollectionViewCell.h"
#import "HGTwoCollectionViewCell.h"
#import "SecViewController.h"
@interface HGGunCollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, assign) NSInteger i;
@end

@implementation HGGunCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createCollection];
        
        [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(collectionRun) userInfo:nil repeats:YES];
        self.i = 0;
    }
    return self;
}

- (void)collectionRun{
    if (_i < 3) {
        [_collection setContentOffset:CGPointMake(0, (35 * _i)) animated:YES];
        
        _i++;
        
    }else{
        _collection.contentOffset = CGPointMake(0, 0);
        _i = 0;
    }
    
}

- (void)setArr:(NSMutableArray *)arr{
    _arr = arr;
    [_collection reloadData];
}

///创建collectionview
- (void)createCollection{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.itemSize = CGSizeMake(self.frame.size.width - 1, self.frame.size.height - 1);
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:layout];
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:1];
    _collection.pagingEnabled = YES;
    [self addSubview:_collection];
    
    [_collection registerClass:[HGTwoCollectionViewCell class] forCellWithReuseIdentifier:@"HGTwoCollectionViewCell"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HGTwoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HGTwoCollectionViewCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.layer.cornerRadius = 5;
    cell.layer.masksToBounds = YES;
    TVModel *model = _arr[indexPath.row];
    cell.model = model;
    
    return cell;
}
///点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    TVModel *model = _arr[indexPath.row];
    
    [self.delegate jumpToWebVcwithUrl:model.playUrl];

}

@end
